package com.lzy.cloud.shop.controller;

import com.lzy.cloud.shop.dto.ShopDto;
import com.lzy.cloud.shop.facade.ShopFacade;
import com.lzy.cloud.shop.request.ShopRequest;
import com.lzy.cloud.shop.response.ShopResponse;
import com.lzy.cloud.shop.service.ShopService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/shop")
@RestController
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopFacade shopFacade;

    /**
     * 分页查询各个模块的商家（分页）(搜索条件：所在地，铺名车，联系人，联系方式)
     * @param request
     * @return
     */
    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    public Result selectShopList(ShopRequest request){
        Ensure.that(request).isNotNull("00700001");
        Ensure.that(request.getPageNum()).isNotNull("");
        Ensure.that(request.getPageSize()).isNotNull("");
        Ensure.that(request.getCategoryId()).isNotNull("");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        List<ShopDto> shopDtoList = shopFacade.selectShopList(pager,DozerUtils.map(request,ShopDto.class));
        List<ShopResponse> shopResponseList = new ArrayList<>();
        if(shopDtoList != null && shopDtoList.size()> 0){
            pager.setTotal(Long.valueOf(shopDtoList.size()));
            shopResponseList = DozerUtils.maps(shopDtoList,ShopResponse.class);
        }
        return new Result(pager,shopResponseList);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}" ,method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id){
        ShopResponse shopResponse = null;
        Ensure.that(id).isNotNull("00700001");
        ShopDto shopDto = shopFacade.detail(id);
        if(shopDto != null){
            shopResponse = DozerUtils.map(shopDto,ShopResponse.class);
        }
        return new Result(shopResponse);
    }

    /**
     * 根据id单表查询shop
     * @param id
     * @return
     */
    @RequestMapping(value = "/find/{id}" ,method = RequestMethod.GET)
    public Result findById(@PathVariable("id") Long id){
        Ensure.that(id).isNotNull("00700001");
        ShopResponse shopResponse = null;
        ShopDto shopDto = shopService.selectById(id);
        if(shopDto != null){
            shopResponse = DozerUtils.map(shopDto,ShopResponse.class);
        }
        return new Result(shopResponse);
    }

    /**
     * 添加店铺
     * @param request
     * @return
     */
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public Result add(@RequestBody ShopRequest request){
        Ensure.that(request).isNotNull("00700001");
        return new Result(shopFacade.add(DozerUtils.map(request,ShopDto.class)));
    }

    /**
     * 更新店铺
     * @param request
     * @return
     */
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    public Result update(@RequestBody ShopRequest request){
        Ensure.that(request).isNotNull("00700001");
        return new Result(shopService.updateShop(DozerUtils.map(request,ShopDto.class)));
    }
}
